# -*- coding:utf-8 -*-
import ast
from redis_op import Operator

def all(key):
    op = Operator()
    ks = op.hgetall(key)
    d = {}
    for k in ks:
        try:
            v = ast.literal_eval(op.hget(key, k))
            d[k] = v
        except Exception as e:
            raise Exception(u'车辆属性又错了', e)
    return d


def flat_all(key):
    field = all(key)
    l = []
    for k, v in field.items():
        d = {}
        value, sort = v
        d['id'] = k
        d['value'] = value
        d['sort'] = sort
        l.append(d)
    l.sort(key=lambda x: int(x['sort']))
    return l
